#coding:utf-8
import sys
import os
import json
from algo.similarity import *
from algo.predict import *

def is_unicode_hanzi(c):
    d = ord(c)
    #基本汉字
    if d >= 0x4E00 and d <= 0x9FA5:
        return True
    #补充汉字
    if d >= 0x9FA6 and d <= 0x9FCB:
        return True
    #扩展A
    if d >= 0x3400 and d <= 0x4DB5:
        return True
    #兼容汉字
    if d >= 0xF900 and d <= 0xFAD9:
        return True
    return False

def cleanstr( name ):
    if isinstance(name, str):
        name = name.decode('utf-8', 'ignore')
    words = []
    nums = []
    for ch in name:
        if ch == u'之': continue
        if is_unicode_hanzi(ch): 
            words.append(ch.encode('utf-8', 'ignore'))
            continue
        if ch.isdigit(): 
            ###nums.append( ch.encode('utf-8', 'ignore') )
            words.append( ch.encode('utf-8', 'ignore') )
            continue
        if ch.isalnum():
            words.append( ch.encode('utf-8', 'ignore') )
    if len(words) == 0:
        return ''.join(  nums )
    return ''.join( words )

class VideoSource(object):
    def __init__(self, domain ):
        self._domain = domain
        self._key_maps = {}
        self._allitems = []
        self._tit_indexes = {}

        fname = 'align/result/%s.cfg' % domain
        f = open( fname, 'rb')
        for line in f:
            line = line.strip()
            if line == '': continue
            fields = line.split("\t")
            if len(fields) < 2: continue
            if line.startswith('#'): continue
            ori, dst = fields[:2]
            if dst not in self._key_maps:
                self._key_maps[ dst ] = []
            self._key_maps[ ori ] = dst
        f.close()

    def getIndexes(self):
        return self._tit_indexes

    def getItem(self, idx ):
        return self._allitems[idx]

    def searchTitle(self, key ):
        if key in self._tit_indexes:
            return self._tit_indexes[key]
        return [] 
    def index_director(self):
        self._tit_indexes = {}
        num = len(self._allitems)
        for i in range(num):
            item = self._allitems[i]
            year = item.get('screen_year', '')
            if isinstance(year, int): year = str(year)
            year = dateTool.extract_str( year )
            if not year:
                year = item.get('screen_time', '')
                year = dateTool.extract_str( year )
            year = year[:4]
            if not year: continue
			
            name = item.get('director_name', '')
            if not name: continue
            if isinstance(name, unicode): name = name.encode('utf-8', 'ignore')

            name = name.replace('导演','').replace('(女)', '').replace('/', ';').replace('、', ';')
            names = name.replace('/',';').replace('|', ';').replace(',',';').split(';')
            for name in names:
                name = name.strip()
                name = cleanstr(name)
                if name == '_': continue
                if len(name) <= 2: continue
                key = year + "_" + name
                if key not in self._tit_indexes:
                    self._tit_indexes[key] = []
                self._tit_indexes[ key ].append( i ) 
        return True
    def index_tile(self):
        self._tit_indexes = {}
        pa = VideoNameParser()
        num = len(self._allitems)
        for i in range(num):
            item = self._allitems[i]
            name = item.get('name', '')
            if not name: continue
            words, info = pa.parse_words( name )
            if not words: 
                continue
            newwords = []
            for w in words:
                word = w['word'].strip()
                if word == '': continue
                newwords.append( word )
            for w in info:
                if info[w] == 200:
                    newwords.append( w )
            if len(newwords) == 0:
                for w in info:
                    if w in ['国语', '粤语']: continue
                    newwords.append( w )
            newwords.sort()
            key = ''.join( newwords )
            key = cleanstr( key )
            
            if not key: 
                continue
            if key not in self._tit_indexes:
                self._tit_indexes[key] = []
            self._tit_indexes[ key ].append( i ) 
        
        return True
    def loadfile(self, fname, mstkeys = []):
        f = open(fname, 'rb')
        for line in f:
            line = line.strip()
            if line == '': continue
            data = {}
            try:
                data = json.loads( line )
            except Exception, e:
                #print e
                continue
            item = {}
            for key in data:
                val = data[key]
                if isinstance(val, str) or isinstance(val, unicode):
                    val = val.strip()
                elif isinstance(val,int):
                    val = str(val)
                else: continue

                if isinstance(key, unicode): key = key.encode('utf-8', 'ignore')
                if not val: continue
                if key in self._key_maps:
                    key = self._key_maps[ key ]

                item[key] = val
            delflag = False
            for key in mstkeys:
                
                if key not in item:
                    delflag = True
                    break
                if not item[key]:
                    delflag = True
                    break
            if delflag : 
                continue
            if not item: continue
            #if mstkeys:
                #print item['name'].encode('utf-8', 'ignore'), "\t",item['name_ot'].encode('utf-8', 'ignore')
            self._allitems.append( item ) 
        f.close()

		
def toJson( xitem ):
    item = {}
    for key in xitem:
        val = xitem[key]
        if isinstance(val, unicode): 
            val = val.encode('utf-8', 'ignore')
        elif isinstance(val, int):
            val = str(val)
        if isinstance(key, unicode):
            key = key.encode('utf-8', 'ignore')

        item[key] = val
    return json.dumps( item, ensure_ascii = False )

def match( dou, les ):
    tit_indexes = les.getIndexes()
    for key in tit_indexes:
        oriIdxs = tit_indexes[key]
        if not oriIdxs: continue
        
        dstIdxs = dou.searchTitle( key )
        
        if not dstIdxs: continue
        for xiid in oriIdxs:
            xitem = les.getItem( xiid )
            for yiid in dstIdxs:
                #print 'rrrr'
                yitem = dou.getItem( yiid )
                calc_sim = Similarity(yitem,xitem) 
                features = calc_sim.get_features()
                #print features
                prob = gbrt_model.predict( features )
                #print prob
                #print features
                #if prob >= gbrt_model.get_limit():
                #print prob
                if prob >= 0.48:
                    x = toJson(xitem)
                    y = toJson(yitem)
                    print x + "\t" + y+"\t"+str(prob)
                    #print 'ok'
                else:
                    #print 'err'
                    continue
                #featurestr = json.dumps( features, ensure_ascii = False )
                #print featurestr
                #sim = pass
                pass
            pass
        pass
    pass


def run_match():
    fname = 'align/douban.dat'
    dou = VideoSource('douban.com')
	
    #fname = 'align/dianshiju.json'
    #dou = VideoSource('baidu.com')
    dou.loadfile( fname, ['name_ot'])

    #dou.index_tile()
    dou.index_director()
    #fname = 'align/letv.json.2'
    #les = VideoSource('letv.cn')

    fname = 'align/net.json'
    les = VideoSource('net.cn')
    
    les.loadfile( fname )
    #les.index_tile()
    les.index_director()
	
    match( dou, les )

if __name__ == '__main__':
    run_match()
    #run_lose_match()
